package com.ycy.query;

import com.ycy.BaseTest;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.common.engine.api.query.QueryProperty;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ProcessQuery extends BaseTest {

  //查询流程定义
  @Test
  public void querypro() {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    //获取流程实例中的task
    List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
            .list();
    for (ProcessDefinition processDefinition : list) {
      System.out.println(processDefinition);
      String description = processDefinition.getDescription();
      System.out.println(description);
    }
  }

  //查询流程实例 历史
  @Test
  public void test2() {
    List<HistoricProcessInstance> lists = processEngine.getHistoryService()
            .createHistoricProcessInstanceQuery()
//            .processDefinitionKey("test_subProcess")
            .unfinished()
            .orderByProcessInstanceStartTime()
            .desc()
            .list();
    for (HistoricProcessInstance historicProcessInstance : lists) {
      System.out.println(historicProcessInstance + " " + historicProcessInstance.getEndTime() + " " + historicProcessInstance.getId() + " " + historicProcessInstance.getSuperProcessInstanceId());
    }
  }

  @Test
  public void query() {
    String processDefinitionId = "skiping:1:3";
    processDefinitionId = "test_subProcess:4:57503";
    System.out.println("开始查询");
    BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
    System.out.println(bpmnModel);
    //获取其实起始节点
    Process process = bpmnModel.getProcesses().get(0);
    Collection<FlowElement> flowElements = process.getFlowElements();
    for (FlowElement flowElement : flowElements) {
      if (flowElement instanceof StartEvent) {
        System.out.println(flowElement + " " + flowElement.getDocumentation());
      }
    }
  }

  @Test
  public void query1() {
    ProcessDefinition re =
            processEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("skiping").latestVersion().singleResult();
    System.out.println(re);
  }

  @Test
  public void query11() {
    List<HistoricProcessInstance> lists = processEngine.getHistoryService()
            .createHistoricProcessInstanceQuery()
            .orderByProcessInstanceStartTime()
            .desc()
            .list();
    for (HistoricProcessInstance list : lists) {
      System.out.println(list);
    }
  }

}
